package com.zky.gulimall.cart.controller;

import com.zky.gulimall.cart.service.CartService;
import com.zky.gulimall.cart.vo.Cart;
import com.zky.gulimall.cart.vo.CartItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;


/**
 * @Description 购物车
 * @Author 张开源
 * @Date 2022/12/5 23:08
 * @Version 1.0
 **/
@Controller
@Slf4j
public class CartController {

    @Resource
    private CartService cartService;

    /**
     * 购物车页面   浏览器有个cookie，user-key标识用户身份，一个月后过期，首次使用购物车也会分配一个临时的user-key
     *
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCart();
        log.warn("cartListPage getCart  cart:{}", cart);
        model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 添加购物车
     *
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        log.warn("skuId:{},num:{}", skuId, num);
        cartService.addToCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.zky.com/addToCartSuccess.html";
    }


    /**
     * 重定向到购物车成功页,解决重复提交问题，同意页面刷新问题(不解决复制链接到不同窗口去操作，京东也是)
     *
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        //重定向到购物车成功页再查询一下数据
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item", cartItem);
        return "success";
    }


    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("check") Integer check) {
        cartService.checkItem(skuId, check);
        return "redirect:http://cart.zky.com/cart.html";
    }


    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("count") Integer count) {
        cartService.changeCartItemCount(skuId, count);
        return "redirect:http://cart.zky.com/cart.html";
    }


    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteCartItemBySkuId(skuId);
        return "redirect:http://cart.zky.com/cart.html";
    }

    @GetMapping("/currentMemberCartItem")
    @ResponseBody
    public List<CartItem> currentMemberCartItem() {
        return cartService.userCartItems();
    }
}
